/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-6-17 下午5:36
 * Copyright (c) 2017. All rights reserved.
 */

package com.hack.eden.pdfreader.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.hack.eden.pdfreader.R;

import java.util.ArrayList;
import java.util.List;

public class PersonalFragment extends Fragment implements View.OnClickListener{


    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_personal, container, false);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
        view1 = inflater.inflate(R.layout.lay1, null);
        view2 = inflater.inflate(R.layout.lay2, null);

        viewList = new ArrayList<>();// 将要分页显示的View装入数组中
        viewList.add(view1);
        viewList.add(view2);

        //初始化指示器位置
        initCursorPos();

        viewPager.setAdapter(new PersonalFragment.MyPagerAdapter(viewList));
        viewPager.setOnPageChangeListener(new PersonalFragment.MyPageChangeListener());

        undoTextView = (TextView) rootView.findViewById(R.id.undo_text_view);
        doneTextView = (TextView) rootView.findViewById(R.id.done_text_view);
        undoTextView.setOnClickListener(this);
        doneTextView.setOnClickListener(this);

        return rootView;
    }

    private View view1, view2;
    private List<View> viewList;// view数组
    private ViewPager viewPager; // 对应的viewPager

    private ImageView cursor;
    private int currIndex = 0;// 当前页卡编号
    private int screenWidth;

    private TextView undoTextView;
    private TextView doneTextView;

    //初始化指示器位置
    public void initCursorPos() {
        // 初始化动画
        cursor = (ImageView) rootView.findViewById(R.id.cursor_view);

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        screenWidth = dm.widthPixels;// 获取分辨率宽度
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.undo_text_view:

                if (viewPager.getCurrentItem() == 1) {
                    doTraslateAnimation(screenWidth / 2, 0);
                    undoTextView.setTextColor(getResources().getColor(R.color.colorAccent));
                    doneTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
                    viewPager.setCurrentItem(0);
                }

                break;
            case R.id.done_text_view:
                if (viewPager.getCurrentItem() == 0) {
                    doTraslateAnimation(0, screenWidth / 2);
                    undoTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
                    doneTextView.setTextColor(getResources().getColor(R.color.colorAccent));
                    viewPager.setCurrentItem(1);
                }
                break;
        }
    }

    //页面改变监听器
    public class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageSelected(int arg0) {
            switch (arg0) {
                case 0:
                    if (currIndex == 1) {
                        doTraslateAnimation(screenWidth / 2, 0);
                        undoTextView.setTextColor(getResources().getColor(R.color.colorAccent));
                        doneTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
                    } else if (currIndex == 0) {
                        doTraslateAnimation(screenWidth / 2, 0);
                        undoTextView.setTextColor(getResources().getColor(R.color.colorAccent));
                        doneTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
                    }
                    break;
                case 1:
                    if (currIndex == 0) {
                        doTraslateAnimation(0, screenWidth / 2);
                        undoTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
                        doneTextView.setTextColor(getResources().getColor(R.color.colorAccent));
                    } else if (currIndex == 1) {
                        doTraslateAnimation(0, screenWidth / 2);
                        undoTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
                        doneTextView.setTextColor(getResources().getColor(R.color.colorAccent));
                    }
                    break;
            }
            currIndex = arg0;
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }

    private void doTraslateAnimation(float fromXDelta, float toXDelta) {
        Animation animation = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
        animation.setFillAfter(true);// True:图片停在动画结束位置
        animation.setDuration(300);
        cursor.startAnimation(animation);
    }

    /**
     * ViewPager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            // TODO Auto-generated method stub
            return arg0 == arg1;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mListViews.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // TODO Auto-generated method stub
            container.removeView(mListViews.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // TODO Auto-generated method stub
            container.addView(mListViews.get(position));

            return mListViews.get(position);
        }
    }
}
